/*
 * Licensed Materials - Property of IBM
 * 5725-G92 (C) Copyright IBM Corp. 2006, 2013. All Rights Reserved.
 * US Government Users Restricted Rights - Use, duplication or
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */


$(document).ready(function() {
	$("#phone").keydown(function() {
		var curLength = $("#phone").val().length;
		if (curLength >= 11) {
			var phone = $("#phone").val().substr(0,10);
			$("#phone").val(phone);
			alert("phone number is incorrect format!");
		}
	});
	$("#password").keydown(function() {
		var curLength = $("#password").val().length;
		if (curLength >= 20) {
			var phone = $("#password").val().substr(0,19);
			$("#password").val(phone);
			//alert("phone number is incorrect format!");
		}
	});
});

function wlCommonInit() {
	window.customer = {};
	window.customer.id = '1';
	window.backDriInfo = {};
	window.backDriInfo.driverInfo = {};
	window.busy = new WL.BusyIndicator("content", {
		text : "Loading"
	});
	window.showToast = function(msg, delayTime){
		$("<div class='ui-loader ui-overlay-shadow ui-body-e ui-corner-all'>"+msg+"</div>")
		.css({  
			display: "block", 
			opacity: 0.80, 
			position: "fixed",
			padding: "4px 18px",
			"text-align": "center",
			top: "80%"
		})
		.appendTo( $("#page")).delay(1000)
		.fadeOut(delayTime, function(){
			$(this).remove();
		});
	};
    window.initJS = function() {
    	var initOpts = {onSuccess : function() {
//    		alert("initialze success");
    		window.audioJS = WL.JSONStore.get('audioRecord');
    		window.bookNowState = WL.JSONStore.get('bookNowState');
    		
    	}, onFailure: function() {alert("initialze failure");}};
    	//when initialize the project , clear all previous data 
//    	WL.JSONStore.destroy({});
    	var collections = {};
    	collections['audioRecord'] = {};
    	collections['bookNowState'] = {};
    	collections['audioRecord'].searchFields = {cus_id: 'string', dri_id:'string', recordTime: 'integer'};
    	collections['bookNowState'].searchFields = {cus_id: 'string', dri_id: 'string'};
    	WL.JSONStore.init(collections, initOpts);
    	
    };
    window.initJS();
}
/**
 * after tap the mic start record or stop record
 */
function controlRecord() {
	alert("control record");
	var data1 = {cus_id: '1', dri_id:'0', recordTime : 1, encodedString: "recording baiyushi1"};
	var data2 = {cus_id: '1', dri_id:'123', recordTime : 11, encodedString: "recording baiyushi2"};
	var data3 = {cus_id: '1', dri_id:'0', recordTime : 111, encodedString: "recording baiyushi3"};
	var data4 = {cus_id: '1', dri_id:'0', recordTime : 1111, encodedString: "recording baiyushi1"};
	var data5 = {cus_id: '1', dri_id:'123', recordTime : 11111, encodedString: "recording baiyushi2"};
	var data6 = {cus_id: '1', dri_id:'0', recordTime : 111111, encodedString: "recording baiyushi3"};
	var data7 = {cus_id: '1', dri_id:'0', recordTime : 1111111, encodedString: "recording baiyushi1"};
	var data8 = {cus_id: '1', dri_id:'123', recordTime : 11111111, encodedString: "recording baiyushi2"};
	var data9 = {cus_id: '1', dri_id:'0', recordTime : 111111111, encodedString: "recording baiyushi3"};
	alert("add");
	window.audioJS.add(data1, {onSuccess: function(){alert("insert success");}, onFailure: function(){alert("insert fail");}});
	window.audioJS.add(data2, {});
	window.audioJS.add(data3, {});
	window.audioJS.add(data4, {});
	window.audioJS.add(data5, {});
	window.audioJS.add(data6, {});
	window.audioJS.add(data7, {});
	window.audioJS.add(data8, {});
	window.audioJS.add(data9, {});
	
//	var options = {};
//	window.audioJS.count()
//	.then(function (numberOfDocuments) {
//		var options = {};
//		if(numberOfDocuments > 2) {
////			//if the number of document bigger then 6, take last 6
//			options = {
//					offset: numberOfDocuments - 2,
//				    limit: 2,
//				    onSuccess: function(data) {
//						alert("find success:" + JSON.stringify(data));
//						alert(data.length);
//						alert(data[0].json.encodedString);
//					},
//					onFailure: function(){alert("find fail");}
//			}; 
//		}
//		window.audioJS.findAll(options);
//	})
//	.fail(function (errorObject) {
//		alert("count fail ");
//	});
}
function loadFeeds() {
	var invocationData = {
		adapter : 'typAdp',
		procedure : 'getStories',
		parameters : [{
			'latitude' : '111',
			'longitude' : '333'
		}]
	};

	WL.Client.invokeProcedure(invocationData, {
		onSuccess : loadFeedsSuccess,
		onFailure : loadFeedsFailure
	});
}
function loadFeedsSuccess(result) {
	alert(result.invocationResult.result);
}
function loadFeedsFailure(result) {
	WL.Logger.error("Feed retrieve failure");
}

////-------------------------------------
//var mymedia = null;
//var recordSrc = "smarttaxiaaaa/record.ogg";
//
///**
// * after tap the mic start record or stop record
// */
//function controlRecord() {
//	var currentStatus = $('#driverBtn').data('state');
//	if (currentStatus == "stop") {
//		startRecordAudio();
//		$('#driverBtn').data('state', "running");
//	} else if (currentStatus == "running") {
//		stopRecordAudio();
//		$('#driverBtn').data('state', "stop");
//	}
//}
//
//function startRecordAudio() {
//	if (mymedia == null) {
//		var recAudioSuccess = function() {
////			alert("startRecordAudio():Audio new Success");
//		};
//		var recAudioError = function(err) {
////			alert("startRecordAudio():Audio Error: " + err);
//		};
//		var recAudioStatus = function(status) {
////			alert("startRecordAudio():Audio Status: " + Media.MEDIA_MSG[status]);
//		};
//		mymedia = new Media(recordSrc, recAudioSuccess, recAudioError,
//				recAudioStatus);
//	}
//	alert("startRecord");
//	mymedia.startRecord();
//}
//
//function stopRecordAudio() {
//	mymedia.stopRecord();
//	playRecord();
//}
//
//function playRecord() {
//	var media1 = new Media(window.recordSrc, function() {
////		alert("playAudio():Audio new Success");
//	}, function(err) {
////		alert("playAudio():Audio new Error: " + err);
//	}, function(status) {
////		alert("playAudio():Audio Status: " + Media.MEDIA_MSG[status]);
//	});
//	alert("playRecord");
//	media1.play();
////	var fileLocation = "/data/data/com.SmartTest7_17/files/www/default/record.ogg";
////	alert(fileLocation);
//	cordova.exec(encodeSuccess, encodeFailure, "EncodeUtilPlugin", "encodeAudio", [recordSrc]);
//	
//}
//
//function encodeSuccess(result) {
//	alert("success:" + result);
//	var cus_id = "1";
//	var dri_id = "1";
//	var invocationData = {
//			adapter : 'stAdp',
//			procedure : 'sendAudio',
//			parameters : [{'cus_id': cus_id,'dri_id':dri_id,'encodedString':result}]
//	};
//	WL.Client.invokeProcedure(invocationData, {
//		onSuccess : receiveEncodeAudio,
//		onFailure : decodeFailure
//	});
//}
//
//function receiveEncodeAudio(result) {
//	//alert(result.encodedString);
//	var encodedContent = result.invocationResult.encodedString;
//	cordova.exec(decodeSuccess, decodeFailure, "EncodeUtilPlugin", "decodeAudio", [encodedContent]);
//}
//
//function encodeFailure(result) {
//	alert("fail:" + result);
//}
//
//function decodeSuccess(result) {
//	alert("decodeSuccess");
//}
//
//function decodeFailure(result) {
//	alert("decodeFail:");
//}